{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook data from merged .csv (vibration and temperature) are loaded and processed by 'utils.data_utils' functions. In the end all the processed data is saved in binary format and visualized"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The autoreload extension is already loaded. To reload it, use:\n",
      "  %reload_ext autoreload\n"
     ]
    }
   ],
   "source": [
    "import itertools\n",
    "import numpy as np\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.widgets import Slider\n",
    "\n",
    "from bokeh.plotting import figure\n",
    "from bokeh.io import show\n",
    "from bokeh.palettes import Dark2_5 as palette\n",
    "from bokeh.layouts import row, column\n",
    "\n",
    "\n",
    "from utils import data_model\n",
    "\n",
    "from utils.visualization   import scatter3d_plot\n",
    "\n",
    "from utils.preprocessing_c import derivative\n",
    "\n",
    "from utils.preprocessing   import ( cumsum, hankel_svdvals, correlation_coeffs,\n",
    "                                    fft_spectrogram, step_change_point, rms,\n",
    "                                    imfs_decomposition )\n",
    "\n",
    "\n",
    "%matplotlib notebook \n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_path = 'data/processed_data/femto_dataset'\n",
    "\n",
    "# 'all' or a list of bearings name (e.g. 'all' or ['Bearing1_1', 'Bearing2_5', 'Bearing3_1', ...])\n",
    "bearings_to_load = ['Bearing1_1', 'Bearing2_5', 'Bearing3_1']\n",
    "\n",
    "# 'all' or a list of data names (e.g. 'all' or ['acc', 'temp', 'cumsum', 'fft_spectogram', ...])\n",
    "# results from 'data_utils' functions have the same data name of its corresponding function \n",
    "data_to_load = 'all'\n",
    "\n",
    "bearings = data_model.load(dataset_path, bearings_to_load, data_to_load)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Defining kwargs for preprocessing functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fs = 25600                                 # 25600 Hz\n",
    "recording_time = 0.1                       # 0.1 seconds\n",
    "pts_per_recording = int(fs*recording_time) # 2560 points per recording\n",
    "\n",
    "\n",
    "hankel_svdvals_kw = {'hankel_window_size' : 9,\n",
    "                     'slice_window_size' : pts_per_recording}\n",
    "\n",
    "\n",
    "correlation_coeffs_kw = {'baseline' : 0,\n",
    "                         'norm_interval' : [-1, 1],\n",
    "                         'filter_window_size' : 3,\n",
    "                         'filter_polyorder' : 2}\n",
    "\n",
    "\n",
    "fft_spectrogram_kw = {'slice_window_size' : pts_per_recording, \n",
    "                      'recording_time' : recording_time,\n",
    "                      'fs' : fs}\n",
    "\n",
    "# Some problems here in derivative because the time difference between some data points is not '1/fs' = 39.06 us\n",
    "# because the time difference between the last datapoint of a recording and the first datapoint of the next\n",
    "# recording is 10s\n",
    "derivative_kw = {'h' : 1/fs}\n",
    "\n",
    "\n",
    "rms_kw = {'slice_window_size' : pts_per_recording}\n",
    "\n",
    "\n",
    "imfs_decomp_kw = {'slice_window_size' : pts_per_recording}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for bearing in bearings:\n",
    "    \n",
    "    ### Vibration data\n",
    "    acc_v = bearing.data['acc']['v']\n",
    "    \n",
    "    \n",
    "    ### Preprocessing functions \n",
    "    bearing.data['cumsum_v'] = cumsum(acc_v)\n",
    "    \n",
    "    \n",
    "    bearing.data['hankel_svdvals_v'] = hankel_svdvals(acc_v, **hankel_svdvals_kw)\n",
    "        \n",
    "    \n",
    "    bearing.data['correlation_coeffs_v'] = correlation_coeffs(bearing.data['hankel_svdvals_v'],\n",
    "                                                              **correlation_coeffs_kw)\n",
    "    \n",
    "    ## Some data analysis functions bellow. Not needed for regression\n",
    "    \n",
    "    #bearing.data['fft_spectrogram_v'] = fft_spectrogram(acc_v, **fft_spectrogram_kw)\n",
    "    \n",
    "    \n",
    "    #bearing.data['derivative_v'] = np.asarray(derivative(acc_v.to_numpy(), **derivative_kw))\n",
    "    \n",
    "    \n",
    "    #bearing.data['rms_v'] = rms(acc_v, **rms_kw)\n",
    "    \n",
    "    \n",
    "    #bearing.data['imfs_decomposition_v'] = imfs_decomposition(acc_v, **imfs_decomp_kw)\n",
    "    \n",
    "        \n",
    "    #bearing.data['deriv_step_change_point_v'] = step_change_point(bearing.data['derivative_v'])\n",
    "    \n",
    "    \n",
    "    #bearing.data['cumsum_step_change_point_v'] = step_change_point(bearing.data['cumsum_v'])\n",
    "    \n",
    "    \n",
    "    ### Saving processed data\n",
    "    bearing.save_data()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Processed Data Visualization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cummulative Sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "step = 200\n",
    "s_cumsum = []\n",
    "\n",
    "for bearing in bearings:\n",
    "    \n",
    "    data = bearing.data['cumsum_v'][::step]\n",
    "    \n",
    "    figure_kw = {\n",
    "        'plot_width'  : 500, \n",
    "        'plot_height' : 500, \n",
    "        'title' : 'Cummulative Sum.',\n",
    "        'x_axis_label' : 'Data points, step = %s' % step,\n",
    "        'y_axis_label' : 'Cummulative Sum'\n",
    "    }\n",
    "    \n",
    "    plot_kw = {\n",
    "        'x' : np.arange(len(data)),\n",
    "        'y' : data,\n",
    "        \"legend_label\" : '%s' % (bearing.name)\n",
    "    }\n",
    "    \n",
    "    s = figure(**figure_kw)\n",
    "    s.line(**plot_kw)\n",
    "    s.legend.location = 'bottom_left'\n",
    "    \n",
    "    s_cumsum.append(s)\n",
    "\n",
    "#show(row(column(s_cumsum)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Correlation Coefficients"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_svd = []\n",
    "for bearing in bearings:\n",
    "    \n",
    "    data = bearing.data['correlation_coeffs_v']\n",
    "    \n",
    "    figure_kw = {\n",
    "        'plot_width'  : 500, \n",
    "        'plot_height' : 500, \n",
    "        'title' : 'Hankel matrix singular values correlation coefficients.',\n",
    "        'x_axis_label' : 'Recordings',\n",
    "        'y_axis_label' : 'Correlation coefficients'\n",
    "    }\n",
    "    \n",
    "    plot_kw = {\n",
    "        'x' : np.arange(len(data)),\n",
    "        'y' : data,\n",
    "        \"legend_label\" : '%s' % (bearing.name)\n",
    "    }\n",
    "    \n",
    "    s = figure(**figure_kw)\n",
    "    s.circle(**plot_kw)\n",
    "    s.legend.location = 'bottom_left'\n",
    "    \n",
    "    s_svd.append(s)\n",
    "\n",
    "#show(row(column(s_svd)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## FFT Spectogram 3D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "step = 8\n",
    "x, y, z = bearings[0].data['fft_spectrogram_v']\n",
    "\n",
    "fft_spectogram_kw = {\n",
    "    'filename' : 'fft_spectrogram_3d.html',\n",
    "    'x' : np.hstack(x)[::step], \n",
    "    'y' : np.hstack(y)[::step], \n",
    "    'z' : np.hstack(z)[::step]\n",
    "}\n",
    "\n",
    "\n",
    "scatter3d_plot(**fft_spectogram_kw)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## RMS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_rms = []\n",
    "\n",
    "for bearing in bearings:\n",
    "    \n",
    "    data = bearing.data['rms_v']\n",
    "    \n",
    "    figure_kw = {\n",
    "        'plot_width'  : 500, \n",
    "        'plot_height' : 500, \n",
    "        'title' : 'RMS.',\n",
    "        'x_axis_label' : 'Recordings',\n",
    "        'y_axis_label' : 'RMS'\n",
    "    }\n",
    "    \n",
    "    plot_kw = {\n",
    "        'x' : np.arange(len(data)),\n",
    "        'y' : data,\n",
    "        \"legend_label\" : '%s' % (bearing.name)\n",
    "    }\n",
    "    \n",
    "    s = figure(**figure_kw)\n",
    "    s.line(**plot_kw)\n",
    "    s.legend.location = 'bottom_left'\n",
    "    \n",
    "    s_rms.append(s)\n",
    "\n",
    "#show(row(column(s_rms)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/tmp/tmp3cypxeat.png'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from bokeh.io import export_png\n",
    "export_png(row(column(s_cumsum), column(s_svd), column(s_rms)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## IMFS Decomposition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = bearings[0].data['imfs_decomposition_v']\n",
    "imf_num = 3\n",
    "\n",
    "imf = [x[imf_num] for x in data]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Update data with slider\n",
    "def update(val):\n",
    "    data = imf[int(s_recording.val)]\n",
    "    l.set_ydata(data)\n",
    "    fig.canvas.draw_idle()\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "# Configure the limit if data is not appearing\n",
    "ax.set_ylim(-2, 2)\n",
    "plt.subplots_adjust(left=0.25, bottom=0.25)\n",
    "plt.title(\"IMF %s\" % imf_num)\n",
    "\n",
    "# Plotting the first recording result\n",
    "l, = plt.plot(np.arange(2560), imf[0], lw=0.7)\n",
    "ax.margins(x=0)\n",
    "\n",
    "axcolor = 'lightgoldenrodyellow'\n",
    "ax_recording = plt.axes([0.25, 0.1, 0.6, 0.03], facecolor=axcolor)\n",
    "\n",
    "s_recording = Slider(ax_recording, 'Recording', 0, len(imf), valinit=0, valstep=1)\n",
    "s_recording.on_changed(update)\n",
    "\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "bearing_rul_predict",
   "language": "python",
   "name": "bearing_rul_predict"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
